/*
* Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: nfcaiwengine.cpp.
*/

#include <AiwServiceHandler.h>
#include <s32mem.h>
#include <QtCore/QDebug>
#include "nfcaiwengine.h"
#include "nfcaiwwrapper.h"

_LIT8(KContentType,"*");

CNfcAiwEngine::CNfcAiwEngine( NfcAiwWrapper *aAiwWrapper ) :
    iAiwWrapper( aAiwWrapper )
    {
    }

CNfcAiwEngine* CNfcAiwEngine::NewL( NfcAiwWrapper *aAiwWrapper )
    {
    CNfcAiwEngine* self = new (ELeave) CNfcAiwEngine( aAiwWrapper );
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop();
    return self;
    }

void CNfcAiwEngine::ConstructL()
    {
    iServiceHandler = CAiwServiceHandler::NewL();
    }

CNfcAiwEngine::~CNfcAiwEngine()
    {
    delete iServiceHandler;
    }

void CNfcAiwEngine::StartEasySetupL()
{
    RCriteriaArray interest;
    CleanupClosePushL(interest);

    CAiwCriteriaItem* criteria = CAiwCriteriaItem::NewLC( 20001,
                                                          KAiwCmdNFCEasySetup,
                                                          KContentType );
    criteria->SetServiceClass( TUid::Uid(KAiwClassBase) );
    User::LeaveIfError( interest.Append(criteria) );

    // Attach to AIW interest.
    iServiceHandler->Reset();
    iServiceHandler->AttachL(interest);

    iServiceHandler->ExecuteServiceCmdL( KAiwCmdNFCEasySetup,
                                         iServiceHandler->InParamListL(),
                                         iServiceHandler->OutParamListL(),
                                         KAiwOptASyncronous,
                                         this );

    CleanupStack::PopAndDestroy(criteria);
    CleanupStack::PopAndDestroy(&interest);
}

TInt CNfcAiwEngine::HandleNotifyL( TInt aCmdId,
                                   TInt aEventId,
                                   CAiwGenericParamList& aEventParamList,
                                   const CAiwGenericParamList& /*aInParamList*/ )
{
    qDebug() << "CNfcAiwEngine::HandleNotifyL - command id: " << QString( "0x%1" ).arg( aCmdId, 2, 16 );
    qDebug() << "CNfcAiwEngine::HandleNotifyL - event id: " << aEventId;

    if ( aCmdId == KAiwCmdNFCEasySetup && aEventId == KAiwEventStarted )
    {
        // This statement filters off unnecessary callbacks from NfcAiwProvider.
        if ( !aEventParamList.Count() )
        {
            qDebug() << "CNfcAiwEngine::HandleNotifyL - aEventParamList empty";
            return KErrNone;
        }

        // Read the NFC radio status - no actions required after this info.
        if ( aEventParamList[0].Value().AsTInt32() == 3 )
        {
            qDebug() << "CNfcAiwEngine::HandleNotifyL - int param: " << aEventParamList[0].Value().AsTInt32();
            return KErrNone;
        }

        // Read the Bluetooth information.
        TAiwGenericParam param = aEventParamList[0];
        QByteArray info = QByteArray( reinterpret_cast<const char*>( param.Value().AsData().Ptr() ),
                                                                     param.Value().AsData().Length());

        if ( info.length() )
        {
            qDebug() << "CNfcAiwEngine::HandleNotifyL - data param - length: " << info.length();
            QT_TRYCATCH_LEAVING(emit(iAiwWrapper->connectionInfoReceived( info )));
        }
        // Got the BT info - no need to continue to pairing.
        iServiceHandler->Reset();
    }
    else
    {
        qDebug() << "CNfcAiwEngine::HandleNotifyL - Service closed.";
        iServiceHandler->Reset();
    }
    return KErrNone;
}
